package cn.doitedu.day05

import org.apache.spark.{SparkConf, SparkContext, TaskContext}

import java.net.InetAddress

object T09_SerTest06 {

  def main(args: Array[String]): Unit = {

    val conf = new SparkConf().setAppName(this.getClass.getSimpleName)
    val sc = new SparkContext(conf)

    val lines = sc.textFile(args(0))

    lines.mapPartitions(it => {
      val rulesMapClassNotSer = new RulesMapClassNotSer
      it.map(line => {
        val fields = line.split(",")
        val cid = fields(0).toInt
        val money = fields(1).toDouble
        val name = rulesMapClassNotSer.rules.getOrElse(cid, "未知")
        //获取当前程序执行时对应的主机名
        val hostname = InetAddress.getLocalHost.getHostName
        //获取当前Task的分区编号
        val partitionId = TaskContext.getPartitionId()
        //获取当前线程Id
        val threadId = Thread.currentThread().getId
        (cid, name, money, hostname, partitionId, threadId, rulesMapClassNotSer)
      })
    }).saveAsTextFile(args(1))

    sc.stop()
  }

}
